import Url  from './config.js'

const http ={
	request(config){
		config=beforeRequest(config)
		config.url=Url.baseUrl+config.url
		config.header={
			'Content-Type':'application/json',
			withCredentials: true
			// cookie: uni.getStorageSync('cookie')
		}
		return new Promise((resolve,reject)=>{
			uni.request(config).then(
			res=>{
				// let [error,resp] =res
				const response =beforeResponse(res)
				resolve(response)
			}).catch(err=>{
				errorHandle()
				reject(err)
			})
		})
	},
	get(url){
		return this.request({
			url:url,
			method:"GET"
		})
	},
	post(url,data){
		return this.request({
			url:url,
			data:data,
			method:"POST"
		})
	},
	delete(url){
		return this.request({
			url:url,
			method:"DELETE"
		})
	},
	put(url,data){
		return this.request({
			url:url,
			data:data,
			method:"PUT"
		})
	}
	
}

//请求拦截器
const beforeRequest=(config)=>{
	
	//请求之前的操作
	console.log('请求拦截器:',config);
	return config
}


//响应拦截器
const beforeResponse=(response)=>{
	
	console.log('响应拦截器:',response);
	return response
}

//异常处理器
const errorHandle =(err)=>{
	console.log('网络异常，请求失败！',err);
}

export default http